function format(date, formatStr) {
  let map = {
    'y': true,
    'M': true,
    'd': true
  }
  let subStr = Array.prototype.find.call(formatStr, e => !map[e])
  let formatArr = formatStr.split(subStr)
  let res = ''
  for (let i = 0; i < formatArr.length; i++) {
    let f = formatArr[i]
    switch (f[0]) {
      case 'y':
        res += `${subStr}${date.getFullYear()}`
        break;
      case 'M':
        let m = date.getMonth() + 1
        if (f.length === 1) {
          res += `${subStr}${m}`
        } else {
          res += `${subStr}0${m}`
        }
        break;
      case 'd':
        let d = date.getDate()
        if (f.length === 1) {
          res += `${subStr}${d}`
        } else {
          res += `${subStr}${d < 10 ? '0' : ''}${d}`
        }
        break;
      default: break;
    }
  }
  print(res.slice(1))
  return res
}
format(new Date('2021/9/2'), 'yyyy-MM-dd')
function print(val) {
  console.log(val);
}